<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('资产管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('资产编号')" prop="assetNumber">
        <el-input
          v-model="queryParams.assetNumber"
          :placeholder="$t('资产编号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('序列号')" prop="assetNumber">
        <el-input
          v-model="queryParams.serialNumber"
          :placeholder="$t('序列号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('所属公司')" prop="company">
        <el-input
          v-model="queryParams.company"
          :placeholder="$t('所属公司')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('资产名称')" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          :placeholder="$t('资产名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('资产状态')" prop="assetState">
        <el-select v-model="queryParams.assetState" :placeholder="$t('资产状态')" clearable>
          <el-option
            v-for="dict in assetStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('存放地点')" prop="storageLocation">
        <el-input
          v-model="queryParams.storageLocation"
          :placeholder="$t('存放地点')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('使用人')" prop="userId">
        <UserSelect v-model="queryParams.userId" :placeholder="$t('使用人')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:assets:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:assets:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="oaAssetsList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('资产编号')" align="center" prop="assetNumber" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('所属公司')" align="center" prop="company" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('采购日期')" align="center" prop="assetTime" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assetTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('资产名称')" align="center" prop="assetName" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.assetName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('规格型号')" align="center" prop="assetModel" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('序列号')" align="center" prop="serialNumber" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('数量')" align="center" prop="assetCount" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('单价')+ $t('(元)')" align="center" prop="assetPrice" min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.assetPrice) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('金额')+ $t('(元)')" align="center" prop="assetAmount" min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.assetAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('资产状态')" align="center" prop="assetState" :formatter="assetStateFormat" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('存放地点')" align="center" prop="storageLocation" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" fixed="right" min-width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['oa:assets:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['oa:assets:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <oaAssetsForm ref="oaAssetsForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listOaAssets, delOaAssets } from '@/api/oa/assets/oaAssets'
import oaAssetsForm from './oaAssetsForm.vue'
export default {
    components: {
        oaAssetsForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 资产管理表格数据
          oaAssetsList: [],
          // 弹出层标题
          title: '资产管理',
          // 是否显示弹出层
          formOpen: false,
        // 资产状态字典
        assetStateOptions: [],

          // 查询参数
          queryParams: {
              assetNumber: undefined,
              company: undefined,
              storageLocation: undefined,
              assetName: undefined,
              assetState: undefined,
              pageNum: 1,
              pageSize: 10,
              userId: undefined
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('asset_state').then(response => {
          this.assetStateOptions = response.data
      })
  },
  methods: {
    /** 查询资产管理列表 */
    getList() {
        this.loading = true
        listOaAssets(this.queryParams).then(response => {
            this.oaAssetsList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 资产状态字典翻译
    assetStateFormat(row) {
      return this.selectDictLabel(this.assetStateOptions, row.assetState)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,

            assetNumber: undefined,

            assetTime: undefined,

            assetName: undefined,

            assetModel: undefined,

            assetCount: undefined,

            assetPrice: undefined,

            assetAmount: undefined,

            assetState: undefined,

            delFlag: undefined,

            createBy: undefined,

            createTime: undefined,

            updateBy: undefined,

            updateTime: undefined

        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.oaAssetsForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.oaAssetsForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delOaAssets(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/assets/oaAssets/export', this.queryParams)
    }
  }
}
</script>
